Security News
New Python Packaging Proposal Aims to Solve Phantom Dependency Problem with SBOMs
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.
@types/invariant
Advanced tools
TypeScript definitions for invariant
The @types/invariant package provides TypeScript type definitions for the invariant npm package. Invariant is a utility that can be used to assert that a certain condition is true. If the condition is false, it will throw an error. The @types/invariant package doesn't contain functionality by itself but adds type support for TypeScript users using the invariant package.
Type Definitions for invariant
This code sample shows how you can use the invariant function with TypeScript type definitions provided by @types/invariant. It asserts that the divisor is not zero before performing the division.
import invariant from 'invariant';
function divide(dividend: number, divisor: number): number {
invariant(divisor !== 0, 'Division by zero.');
return dividend / divisor;
}
divide(10, 2); // works fine
divide(10, 0); // throws error with message 'Division by zero.'
The assert package is a part of Node.js core modules and provides a simple set of assertion tests. It is similar to invariant in that it is used to test expressions for truthiness and throws an AssertionError if the expression evaluates to false. Unlike @types/invariant, assert is not just type definitions but an actual implementation of assertion functions.
Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework. It offers more expressive assertions and expectation methods than invariant. Chai does not need separate type definitions as it includes its own TypeScript support.
Expect.js is a minimalistic BDD-style assertions library that can be used in place of invariant for more expressive assertions. It is similar in purpose but provides a richer API for writing tests. Expect.js does not have separate type definitions and is not as TypeScript-friendly as @types/invariant.
npm install --save @types/invariant
This package contains type definitions for invariant (https://github.com/zertosh/invariant).
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/invariant.
declare let invariant: invariant.InvariantStatic;
export = invariant;
export as namespace invariant;
declare namespace invariant {
interface InvariantStatic {
(testValue: false, format: string, ...extra: any[]): never;
(testValue: any, format: string, ...extra: any[]): asserts testValue;
}
}
These definitions were written by MichaelBennett, dtinth, and Turadg Aleahmad.
FAQs
TypeScript definitions for invariant
The npm package @types/invariant receives a total of 563,862 weekly downloads. As such, @types/invariant popularity was classified as popular.
We found that @types/invariant demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.
Security News
Socket CEO Feross Aboukhadijeh discusses open source security challenges, including zero-day attacks and supply chain risks, on the Cyber Security Council podcast.
Security News
Research
Socket researchers uncover how threat actors weaponize Out-of-Band Application Security Testing (OAST) techniques across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.